package com.xyht.sca_s.student_manage_system.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.PinYinUtil;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsApplication;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsApplicationType;
import com.xyht.sca_s.student_manage_system.modules.user.entity.req.SmsApplicationTypeReq;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsApplicationTypeResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsApplicationMapper;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsApplicationTypeMapper;
import com.xyht.sca_s.student_manage_system.modules.user.service.SmsApplicationTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 应用类型管理 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-04-17
 */
@Service
public class SmsApplicationTypeServiceImpl extends ServiceImpl<SmsApplicationTypeMapper, SmsApplicationType> implements SmsApplicationTypeService {
    @Autowired
    private PinYinUtil pinYinUtil;
    @Autowired
    private SmsApplicationTypeMapper smsApplicationTypeMapper;
    @Autowired
    private SmsApplicationMapper smsApplicationMapper;


    @Override
    public ResponseResult getApplicationType(String search_str, Integer pageNum, Integer pageSize) {
        QueryWrapper<SmsApplicationType> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(search_str)) {
            queryWrapper.lambda().like(SmsApplicationType::getTypeName, search_str);
        }
        queryWrapper.lambda().orderByAsc(SmsApplicationType::getTypeSpell);
        Page<SmsApplicationType> page = new Page<>(pageNum, pageSize);
        smsApplicationTypeMapper.selectPage(page, queryWrapper);
        List<SmsApplicationTypeResp> list = page.getRecords()
                .stream()
                .map(type -> {
                    SmsApplicationTypeResp typeResp = new SmsApplicationTypeResp();
                    BeanUtils.copyProperties(type, typeResp);
                    return typeResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }

    @Override
    public ResponseResult addApplicationType(SmsApplicationTypeReq smsApplicationTypeReq) {
        if (isNullOrEmpty(smsApplicationTypeReq.getTypeName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        List<SmsApplicationType> typeList = smsApplicationTypeMapper.selectList(new QueryWrapper<SmsApplicationType>()
                .lambda()
                .eq(SmsApplicationType::getTypeName, smsApplicationTypeReq.getTypeName()));
        if (typeList != null && typeList.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.APP_TYPE_EXIST);
        }

        String nameSpell = pinYinUtil.getHanZiPY(smsApplicationTypeReq.getTypeName());
        SmsApplicationType applicationType = new SmsApplicationType();
        applicationType.setTypeSpell(nameSpell);
        applicationType.setTypeName(smsApplicationTypeReq.getTypeName());
        smsApplicationTypeMapper.insert(applicationType);
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateApplicationType(SmsApplicationTypeReq smsApplicationTypeReq) {
        if (isNullOrEmpty(smsApplicationTypeReq.getId()) || isNullOrEmpty(smsApplicationTypeReq.getTypeName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsApplicationType> typeList = smsApplicationTypeMapper.selectList(new QueryWrapper<SmsApplicationType>()
                .lambda()
                .eq(SmsApplicationType::getTypeName, smsApplicationTypeReq.getTypeName()));
        if (typeList != null && typeList.size() > 0) {
            return CommonResult.failed(CommonCodeEnum.APP_TYPE_EXIST);
        }

        String nameSpell = pinYinUtil.getHanZiPY(smsApplicationTypeReq.getTypeName());
        SmsApplicationType smsApplicationType = new SmsApplicationType();
        smsApplicationType.setId(smsApplicationTypeReq.getId());
        smsApplicationType.setTypeName(smsApplicationTypeReq.getTypeName());
        smsApplicationType.setTypeSpell(nameSpell);
        smsApplicationTypeMapper.updateById(smsApplicationType);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteApplicationType(SmsApplicationTypeReq smsApplicationTypeReq) {
        if (isNullOrEmpty(smsApplicationTypeReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsApplication> applicationList = smsApplicationMapper.selectList(new QueryWrapper<SmsApplication>()
                .lambda()
                .eq(SmsApplication::getAppTypeId, smsApplicationTypeReq.getId()));
        if (applicationList != null && applicationList.size() > 0){
            return CommonResult.failed(CommonCodeEnum.APP_TYPE_DELETE_FAIL);
        }

        smsApplicationTypeMapper.deleteById(smsApplicationTypeReq.getId());
        return CommonResult.success();
    }
}
